import {defineStore} from "pinia";
import {reqMediaGroupList, reqMediaList} from "@/api";
import {computed, ref} from "vue";


export const useMediaStore = defineStore('media', () => {

    const mediaGroupList = ref([])
    const mediaList = ref([])
    const pageInfo = ref({})

    async function loadMediaGroupList() {
        const resp = await reqMediaGroupList()
        mediaGroupList.value = resp.data
    }

    function getGroupOptionList() {
        return mediaGroupList.value.filter(item => item.id !== -1).map((item) => ({
            label: item.groupName,
            value: item.id
        }))
    }

    async function loadMediaList(params) {
        if (params.groupId === -1) {
            params.groupId = mediaGroupList.value[0].id
        }
        const resp = await reqMediaList(params)
        mediaList.value = resp.data.records
        pageInfo.value = resp.data.pageInfo
    }

    return {
        mediaGroupList,
        mediaList,
        pageInfo,
        loadMediaGroupList,
        getGroupOptionList,
        loadMediaList
    }
})